using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class AprilFools_Manager : MonoBehaviour
{
	public enum AprilFools25_State
	{
		Intro,
		Fight,
		EndingCutscene,
		Hypoaster
	}

	[Header("-= Stats =-")]
	public AprilFools25_State CurrentState;

	[Header("-= References =-")]
	[SerializeField]
	private CHATBOXTEXT IntroText;

	[SerializeField]
	private CHATBOXTEXT GameoverText;

	[SerializeField]
	private CHATBOXTEXT EndingText;

	public AprilFools_PlayerController Player;

	public AprilFools_Longshot Longshot;

	[SerializeField]
	private Image CutsceneFade;

	[SerializeField]
	private GameObject UIHypoaster;

	[SerializeField]
	private GameObject UIGameplay;

	[SerializeField]
	private GameObject UIEnding;

	[SerializeField]
	private Image UIAsgoreIcon;

	[SerializeField]
	private Sprite spr_AsgoreLockedIn;

	[SerializeField]
	private GameObject UILockIn;

	[SerializeField]
	private Transform EndingCutscene_CameraLongshotPos;

	[SerializeField]
	private Transform EndingCutscene_CameraKrisPos;

	[SerializeField]
	private Transform EndingCutscene_CameraAsgorePos;

	[SerializeField]
	private Animator EndingCutscene_Longshot;

	[SerializeField]
	private GameObject EndingCutscene_Rocket;

	[SerializeField]
	private GameObject EndingCutscene_Kris;

	[SerializeField]
	private GameObject EndingCutscene_Asgore;

	[SerializeField]
	private GameObject EndingCutscene_Tunnel;

	[SerializeField]
	private Rigidbody[] EndingCutscene_AsgoreGibs;

	[Header("-= Sounds =-")]
	[SerializeField]
	private AudioClip Song_MagicFrogeIntro;

	[SerializeField]
	private AudioClip Song_STANDOFF_If_It_Was_Good;

	[SerializeField]
	private AudioClip Song_FriendInNeed;

	[SerializeField]
	private AudioClip SFX_AsgoreBreak;

	[SerializeField]
	private AudioSource Song_FriendInNeed2;

	public static AprilFools_Manager instance;

	[Header("-= Stats =-")]
	public int Stat_ShotsFired;

	public int Stat_DamageTaken;

	[SerializeField]
	private float Stat_TimeTaken;

	private bool TimerActive;

	[SerializeField]
	private TextMeshProUGUI StatText;

	[SerializeField]
	private TextMeshProUGUI RankText;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		CurrentState = AprilFools25_State.Intro;
		Player.canMove = false;
		Song_MagicFrogeIntro.LoadAudioData();
		Song_STANDOFF_If_It_Was_Good.LoadAudioData();
		GonerMenu.Instance.ShowMusicCredit("A Stab in The Back", "MagicFroge");
		StartCoroutine(AprilFools());
	}

	private void Update()
	{
		if (TimerActive)
		{
			Stat_TimeTaken += Time.deltaTime;
		}
	}

	public void PlayerGameOver()
	{
		MusicManager.Instance.source.pitch = 1f;
		Player.canMove = false;
		Battle_GameOver.Instance.PossibleDeathMessages = new CHATBOXTEXT[1] { GameoverText };
		Battle_GameOver.Instance.PlayGameOver(CameraManager.instance.transform.position);
		Longshot.gameObject.SetActive(value: false);
		Player.transform.position = new Vector3(100f, 100f, -10f);
		Player.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
		CameraManager.instance.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
		UIGameplay.SetActive(value: false);
		CameraManager.instance.GetComponent<Camera>().orthographic = true;
		CameraManager.instance.GetComponent<Camera>().orthographicSize = 6f;
	}

	public void LockIn()
	{
		Player.walkingSpeed = 20f;
		Player.runningSpeed = 20f;
		Player.Health += 50;
		Player.Health = Mathf.Clamp(Player.Health, 0, 100);
		UILockIn.SetActive(value: true);
		Player.UI_UpdateHPGraphics();
		Player.UI_DisplayHeal();
		CameraManager.instance.GetComponent<Camera>().fieldOfView = 80f;
		Player.ShotgunAnimator.speed = 1.5f;
		UIAsgoreIcon.sprite = spr_AsgoreLockedIn;
		EndingCutscene_Tunnel.transform.position = new Vector3(0f, 4f, 1352f);
		AprilFools_EnvironmentScrolling[] components = GetComponents<AprilFools_EnvironmentScrolling>();
		for (int i = 0; i < components.Length; i++)
		{
			components[i].ScrolLSpeed *= 2f;
		}
	}

	public void EndFight()
	{
		Longshot.gameObject.SetActive(value: false);
		EndingCutscene_Longshot.transform.parent.gameObject.SetActive(value: true);
		Player.canMove = false;
		Player.Health = 999;
		UIGameplay.SetActive(value: false);
		UILockIn.SetActive(value: false);
		EndingCutscene_Tunnel.SetActive(value: false);
		AprilFools_EnvironmentScrolling[] components = GetComponents<AprilFools_EnvironmentScrolling>();
		for (int i = 0; i < components.Length; i++)
		{
			components[i].ScrolLSpeed *= 0f;
		}
		StartCoroutine(EndingCutscene());
	}

	private void OnTriggerEnter(Collider other)
	{
		if ((bool)other.GetComponent<AprilFools_PlayerController>())
		{
			Player.Health = 999;
			PlayerGameOver();
		}
	}

	private IEnumerator AprilFools()
	{
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RunFreshChat(IntroText, 0, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.5f);
		CutsceneUtils.RunFreshChat(IntroText, 1, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(IntroText, 2, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		MusicManager.Instance.source.loop = false;
		MusicManager.PlaySong(Song_MagicFrogeIntro, FadePreviousSong: false, 0f);
		UIHypoaster.SetActive(value: true);
		yield return new WaitForSeconds(4f);
		UIHypoaster.SetActive(value: false);
		CutsceneFade.color = Color.white;
		CutsceneUtils.FadeOutUIImage(CutsceneFade, 4f);
		TimerActive = true;
		MusicManager.Instance.source.loop = true;
		MusicManager.PlaySong(Song_STANDOFF_If_It_Was_Good, FadePreviousSong: false, 0f);
		GonerMenu.Instance.ShowMusicCredit("OFFSTAND", "MagicFroge");
		Player.canMove = true;
		Longshot.BeginLongshotAI();
		CurrentState = AprilFools25_State.Fight;
	}

	private IEnumerator EndingCutscene()
	{
		MusicManager.Instance.source.pitch = 1f;
		MusicManager.StopSong(Fade: false, 0f);
		yield return null;
		Player.canMove = false;
		Player.playerCamera.transform.position = EndingCutscene_CameraLongshotPos.position;
		Player.playerCamera.transform.rotation = EndingCutscene_CameraLongshotPos.rotation;
		CutsceneUtils.RunFreshChat(EndingText, 0, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		MusicManager.PlaySong(Song_FriendInNeed, FadePreviousSong: false, 0f);
		GonerMenu.Instance.ShowMusicCredit("Friend In Need", "Sooski");
		CutsceneUtils.MoveTransformLinear(Player.playerCamera.transform, new Vector3(16.25875f, 12.68045f, -6.437748f), 6f);
		yield return new WaitForSeconds(6f);
		MusicManager.StopSong(Fade: false, 0f);
		Player.playerCamera.transform.position = EndingCutscene_CameraLongshotPos.position;
		Player.playerCamera.transform.rotation = EndingCutscene_CameraLongshotPos.rotation;
		CutsceneUtils.RunFreshChat(EndingText, 1, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		EndingCutscene_Rocket.SetActive(value: true);
		StartCoroutine(WaitForRocketBoom());
	}

	public void EndingCutscene_SwitchToKris()
	{
		Player.playerCamera.transform.position = EndingCutscene_CameraKrisPos.position;
		Player.playerCamera.transform.rotation = EndingCutscene_CameraKrisPos.rotation;
	}

	private IEnumerator WaitForRocketBoom()
	{
		while (EndingCutscene_Rocket != null)
		{
			yield return null;
		}
		EndingCutscene_Kris.SetActive(value: false);
		yield return new WaitForSeconds(3f);
		GonerMenu.Instance.CanOpenGonerMenu = false;
		Player.playerCamera.transform.position = EndingCutscene_CameraAsgorePos.position;
		Player.playerCamera.transform.rotation = EndingCutscene_CameraAsgorePos.rotation;
		EndingCutscene_Asgore.SetActive(value: true);
		yield return new WaitForSeconds(1f);
		Rigidbody[] endingCutscene_AsgoreGibs = EndingCutscene_AsgoreGibs;
		for (int i = 0; i < endingCutscene_AsgoreGibs.Length; i++)
		{
			endingCutscene_AsgoreGibs[i].isKinematic = false;
		}
		CutsceneUtils.PlaySound(SFX_AsgoreBreak);
		string text = "<color=yellow><gradient=\"DarkworldYellow\"><size=90>已打败LONGSHOT</size></color></gradient>\n\n";
		text += $"共开了 - {Stat_ShotsFired} 枪\n";
		text += $"共受伤 - {Stat_DamageTaken} 次\n";
		int num = Mathf.FloorToInt(Stat_TimeTaken / 60f);
		int num2 = Mathf.FloorToInt(Stat_TimeTaken % 60f);
		int num3 = Mathf.FloorToInt(Stat_TimeTaken * 1000f % 1000f);
		text = text + "用时 - " + $"{num:00}:{num2:00}:{num3:000}";
		StatText.text = text;
		string text2 = ((Stat_DamageTaken == 0 && Stat_ShotsFired <= 63 && Stat_TimeTaken <= 150f)
			? "<size=80>评级</size>\r\n<gradient=\"DarkworldYellow\">最棒的副手"
			: ((Stat_DamageTaken > 0 && Stat_ShotsFired < 80 && Stat_TimeTaken < 180f)
				? "<size=80>评级</size>\r\n<color=\"white\">神枪手"
				: ((Stat_DamageTaken <= 20 && Stat_ShotsFired <= 100 && !(Stat_TimeTaken > 240f))
					? "<size=80>评级</size>\r\n<color=\"grey\">捣蛋鬼"
					: "<size=80>评级</size>\r\n<color=\"white\">害虫")));
		RankText.text = text2;
		yield return new WaitForSeconds(5f);
		Song_FriendInNeed2.Play();
		CutsceneUtils.MoveTransformLinear(Player.playerCamera.transform, new Vector3(0.89f, 3.59f, 21.59f), 10f);
		yield return new WaitForSeconds(10f);
		Song_FriendInNeed2.GetComponent<AudioReverbFilter>().enabled = true;
		Song_FriendInNeed2.volume = 0.25f;
		UIEnding.SetActive(value: true);
		yield return new WaitForSeconds(7f);
		Song_FriendInNeed2.volume = 0.667f;
		PlayerPrefs.SetInt("Trophy_Rune", 1);
		while (!Input.anyKeyDown)
		{
			yield return null;
		}
		UIEnding.GetComponent<Animator>().Play("AF25_EndingUI_FadeOut");
		yield return new WaitForSeconds(2.5f);
		UI_FADE.Instance.StartFadeIn(37, 1f, UnpauseOnEnd: true);
	}
}
